#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>

#include "../des/des.h"

#define MAX_DATA 512 
#define MAX_ROWS 100
#define HELP "readme.txt"
#define DBFILE "dbdes.dat"
#define DESKEY "pDE!10&8"
#define DEL() printf("\033[D \033[D")

typedef enum {
    false,
    true
} bool;

bool login(int N)
{
    char name[MAX_DATA];
    char pwd[MAX_DATA];
    int count = 0;
    while(count < N) {
        printf("用户名:");
        cyscanf(name, MAX_DATA, true);
        printf("密码:");
        cyscanf(pwd, MAX_DATA, false);
        if(strcmp(name, "czx") == 0 && strcmp(pwd, "123") == 0)
            break;
        
        count++;
    }

    return count < N ? true : false;
}

int main(int argc, char *argv[])
{
    if(login(3) == false)
    {
        printf("Wrong username or password.\n");
        return 0;
    }
    
    printf("cy> login success.\n");
    char cmd[50];
    int r = 1;
	struct Connection *conn = Database_open();
	while(r) {
        printf("cy> ");
        cyscanf(cmd, 50, true);
        MenuNode *p = FindCmd(menulist, cmd);
        if(NULL == p)
        {
            showhelp(conn);
            continue;
        }

        if(NULL != p->handler)
            r = p->handler(conn);
        else
            printf("%-10s - %s\n", cmd, p->desc);
    }

    Database_close(conn);

    return 0;
}
